/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="dnn.xmlhttp.js" assembly="DotNetNuke.WebUtility" />

Type.registerNamespace('WinDoH.oHtml');

WinDoH.oHtml.View = function() {
    //Call Base Method
    WinDoH.oHtml.View.initializeBase(this);
    //Member Variables
    this._msgs = {};
    this._n = null;
    this._h = null;
    this._w = null;
    this._groupuid = null;
    this._mode = null;
    
    //Associated delegates to single member variable dictionary to make it easy to dispose
    this._delegates = {
        _helloSuccessDelegate: Function.createDelegate(this, this._helloSuccess),
        _helloFailDelegate: Function.createDelegate(this, this._helloFail),
        _onLoadDelegate: Function.createDelegate(this, this._onLoad),
        componentPropChangedDelegate: Function.createDelegate(this, this._onPropChanged)
    };

    //Event Hookup
    Sys.Application.add_load(this._delegates._onLoadDelegate);
}

WinDoH.oHtml.View.prototype =
{
    //Properties
    get_ns: function() { return this.get_id() + '_'; },
    get_msgs: function() { return this._msgs; },
    set_msgs: function(value) { this._msgs = Sys.Serialization.JavaScriptSerializer.deserialize(value); },
    get_GroupUID: function() { return this._groupuid; },
    set_GroupUID: function(value) { this._groupuid = value; },
    get_Mode: function() { return this._mode; },
    set_Mode: function(value) { this._mode = value; },

    //Events
    initialize: function() {
        //Call Base Method
        WinDoH.oHtml.View.callBaseMethod(this, 'initialize');

        switch (this.get_Mode()) {
            case 'accordion':
                this._accordion();
                break;
            default:
                this._tabs();
        }

    },
    _accordion: function() {
        this._w = $get(this.get_ns() + 'wrapper');
        if (this._w) {
            jQuery(this._w).addClass("ui-accordion ui-widget ui-helper-reset ui-accordion-icons");
            var oldthis = this;
            jQuery("#" + this._w.id + " > h3").addClass("ui-accordion-header ui-helper-reset ui-state-default ui-corner-all")
                                          .click(function() {
                                              jQuery("#" + oldthis._w.id + " > div:visible").slideUp('fast').prev().addClass("ui-corner-all").removeClass("ui-corner-top");
                                              jQuery(this).next().slideDown('fast').prev().removeClass("ui-corner-all").addClass("ui-corner-top");
                                          });
            jQuery("#" + this._w.id + " > div").addClass("ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom").hide();

            jQuery("#" + this._w.id + " > div").first().show();
            jQuery("#" + this._w.id + " > h3").first().removeClass("ui-corner-all").addClass("ui-corner-top");
        }


        //$addHandlers(this._w, { "click": this._onNavClick }, this);
    },
    _tabs: function() {
        this._n = $get(this.get_ns() + 'nav');


        if (this._n) {
            this._h = $get(this.get_ns() + 'holder');
            this._w = $get(this.get_ns() + 'wrapper');

            jQuery(this._w).addClass("ui-tabs ui-widget ui-widget-content ui-corner-all");
            jQuery(this._n).addClass("ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all");
            jQuery("#" + this._n.id + " > li").addClass("ui-state-default ui-corner-top");
            jQuery("#" + this.get_GroupUID()).parent().addClass("ui-tabs-selected ui-state-active");

            $addHandlers(this._n, { "click": this._onAccClick }, this);
        }
    },
    _onLoad: function(src, arg) { },
    _onNavClick: function(src, arg) {
        src.preventDefault();
        var targetobj = null;

        jQuery("#" + this._n.id + " > li").removeClass("ui-tabs-selected ui-state-active");

        switch (src.target.tagName) {
            case 'A':
                targetobj = src.target;
                break;
            case 'SPAN':
                targetobj = src.target.parentNode;
                break;
        }
        if (targetobj) {
            jQuery(targetobj.parentNode).addClass('ui-tabs-selected ui-state-active');
            dnn.xmlhttp.callControlMethod('WinDoH.oHtml.View.' + this.get_id(),
            'Display', { indentifier: targetobj.id }, this._delegates._helloSuccessDelegate, this._delegates._helloFailDelegate);
        }
        //alert(targetidentifier);
    },

    _onPropChanged: function(src, args) {
        this.showMessage(String.format('You {0} to {1} but not to me?', args.get_propertyName(), src.get_name()));
    },

    //Methods
    getMessage: function(key) {
        return this._msgs[key];
    },

    showMessage: function(msg) {
        // $get(this.get_ns() + 'lblResponse').innerHTML = msg;
    },

    //Private Methods
    _createChildControl: function(id, tag, type) {
        var ctl = document.createElement(tag);
        ctl.id = this.get_ns() + id;
        if (type)
            ctl.type = type;
        return ctl;
    },

    _displayWait: function(show) {
        //$get(this.get_ns() + 'imgAjax').className = (show ? '' : 'ceHidden');
    },

    _helloSuccess: function(payload, ctx, req) {
        this._displayWait(false);
        //this.showMessage(payload);
        this._h.innerHTML = payload;

    },

    _helloFail: function(payload, ctx, req) {
        this._displayWait(false);
        alert('error: ' + payload);
    },

    dispose: function() {
        $clearHandlers(this._n);
        $clearHandlers(this._h);
        $clearHandlers(this._w);
        this._n = null;
        this._h = null;
        this._w = null;
        this._groupuid = null;

        this._delegates = null;
        WinDoH.oHtml.View.callBaseMethod(this, 'dispose');
    }
}

//register class and inherit from Sys.Component
WinDoH.oHtml.View.registerClass('WinDoH.oHtml.View', Sys.Component);
